package com.etalk.crm.controller.teacher;

import com.etalk.crm.pojo.ClassScheduleInfoMap;
import com.etalk.crm.pojo.TeaComplaintResultMap;
import com.etalk.crm.pojo.TeacherInfoListMap;
import com.etalk.crm.pojo.TeacherInfoResultMap;
import com.etalk.crm.service.LessonsService;
import com.etalk.crm.service.TeacherInfoService;
import com.etalk.crm.service.UserService;
import com.etalk.crm.utils.PojoMapUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jordan
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherInfoService teacherInfoService;
    @Resource
    private LessonsService lessonsService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/info/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String getTeacherInfoListPage(Model model, TeacherInfoListMap record, Integer pageNum, Integer pageSize){
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",teacherInfoService.searchTeacherInfoList(record,pageNum,pageSize));
        return "teacher/teacher_info_list";
    }

    /**
     * 老师名下自动约课学员
     * @param model 对象
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @RequestMapping(value = "/auto/booking/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String getFistTeaAutoBooking(Model model, TeacherInfoResultMap record,Integer pageNum, Integer pageSize){
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",teacherInfoService.searchFistTeaAutoBookingList(record,pageNum,pageSize));
        return "teacher/teacher_auto_booking_list";
    }

    /**
     * 离职老师名下学员
     * @param model 对象
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @RequestMapping(value = "/resign/stu/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String getResignTeacherStudents(Model model, TeacherInfoResultMap record,Integer pageNum, Integer pageSize){
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",teacherInfoService.searchResignTeacherStudentList(record,pageNum,pageSize));
        model.addAttribute("listSsc",userService.sscList());
        return "teacher/teacher_resign_list";
    }

    /**
     * 老师投诉学员列表
     * @param model 对象
     * @param personId 学员id
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @RequestMapping(value = "/complaint/list/{personId}",method = {RequestMethod.GET,RequestMethod.POST})
    public String getTeaComplaintList(Model model, @PathVariable("personId")Integer personId, Integer pageNum, Integer pageSize){
        TeaComplaintResultMap record=new TeaComplaintResultMap();
        record.setPersonId(personId);
        model.addAttribute("personId",personId);
        model.addAttribute("pageInfo",teacherInfoService.searchTeaComplaintListByPage(record,pageNum,pageSize));
        return "teacher/teacher_complaint_list";
    }

    /**
     * 老师排班情况
     * @param model 页面对象
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录数
     * @return
     */
    @RequestMapping(value = "/class/schedule/info",method = {RequestMethod.GET,RequestMethod.POST})
    public String getClassScheduleInfoList(Model model,ClassScheduleInfoMap record, Integer pageNum, Integer pageSize){
        if (record==null){
            record=new ClassScheduleInfoMap();
        }
        if (StringUtils.isEmpty(record.getStrClassDate())){
            record.setStrClassDate(DateFormatUtils.format(new Date(),"yyyy-MM-dd"));
        }
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",teacherInfoService.searchClassScheduleInfoListByPage(record,pageNum,pageSize));
        return "teacher/teacher_schedule_list";
    }

    /**
     * 添加老师投诉处理结果
     * @param lessonId 课程id
     * @param processingResult 处理结果
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/complaint/add/{lessonId}",method = {RequestMethod.POST})
    public Map<String,Object> getTeaComplaintList(@PathVariable("lessonId")Integer lessonId, String processingResult){
        Map<String,Object> resultMap = new HashMap<>(1);
        try {
            int num=lessonsService.updateTeaComplaintByLessonId(lessonId,processingResult);
            if (num > 0){
                resultMap.put("status",1);
            }else {
                resultMap.put("status",0);
            }
        }catch (Exception e) {
            resultMap.put("status",0);
        }

        return resultMap;
    }


}
